﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveCPEWinForm.LiveCPEWS;

namespace LiveCPEWinForm.Gestionnaire
{
	public partial class ucProfile: UserControl
	{
        private GestAPP lGestAPP;

        public ucProfile(GestAPP pGestAPP)
		{
			InitializeComponent();
            // Gestionnaire
            lGestAPP = pGestAPP;
            // Mise à jour du profile EC
            UpdateProfile();
            // Chargement evt
            lGestAPP.UserChanged += new GestAPP.UserChange(lGestAPP_UserChanged);
		}

        void lGestAPP_UserChanged(object sender, GestAPP.ChangeEventUserArgs e)
        {
            UpdateProfile();
        }

        private void UpdateProfile()
        {
            // Mise à jour
            tbUserName.Text = lGestAPP.UserName;
            tbEmail.Text = lGestAPP.user.Email;
            tbFirstName.Text = lGestAPP.user.FirstName.TrimEnd();
            tbLastName.Text = lGestAPP.user.LastName.TrimEnd();
            if (lGestAPP.user.Gender)
                cbGender.Text = "Female";
            else
                cbGender.Text = "Male";
            try
            {
                dtAge.Value = lGestAPP.user.DateOfBirth;
                
            }
            catch (Exception) { };
            // Ajout de l'image de profil
            if (lGestAPP.img != null) pictureBox1.Image = lGestAPP.img;
        }

        private void btnUpgrade_Click(object sender, EventArgs e)
        {
            // MAJ de l'objet à inserer
            UserWS usr = new UserWS();
            usr.UserName = lGestAPP.UserName;
            usr.Email = tbEmail.Text;
            usr.FirstName = tbFirstName.Text;
            usr.LastName = tbLastName.Text;
            usr.DateOfBirth = dtAge.Value;
            usr.Gender = ((string)cbGender.SelectedItem == "Female");
            // Upgrade
            lGestAPP.UpdateUser(usr);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = new Bitmap(open.FileName);
                }
                // Ajout nouvelle image
                lGestAPP.SetImage(lGestAPP.UserName, pictureBox1.Image);
                // Récupération
                pictureBox1.Image = lGestAPP.img;
            }
            catch (Exception)
            {

            }
        }
	}
}
